//
//  AlarmClockViewController.m
//  MedicineBox
//
//  Created by dllo on 15/8/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "AlarmClockViewController.h"
#import "SetTimeViewController.h"
#import "AlarmTableViewCell.h"
#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"
#import "SaveTools.h"
@interface AlarmClockViewController ()<SetTimeViewControllerDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *reciveArray;
@property (nonatomic, retain)  UITableView *tableView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) NSMutableArray *array;
@end

@implementation AlarmClockViewController

-(void)dealloc
{
    [_reciveArray release];
    [_tableView release];
    [_imageView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.reciveArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self createNavigation];
    
    NSString *path = [SaveTools pathWithName:@"alarm" directory:NSDocumentDirectory];
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];
    if (array.count > 0) {
        [self createTable];
    }else
    {
        [self createView];
    }

}


#pragma mark -
#pragma mark 设置导航栏
- (void)createNavigation
{
    
    self.imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.imageView.userInteractionEnabled = YES;
    self.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2.png" ofType:nil]];
    [self.view addSubview:self.imageView];
    [_imageView release];
//标题
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 0.5)];
     [self.imageView addSubview:navView];
     navView .backgroundColor = [UIColor whiteColor];
     navView.nightBackgroundColor = UIColorFromRGB(0x444444);
    [navView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2, 30, 100, 30)];
    titleLabel.text = @"服药提醒";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor orangeColor];
    titleLabel.nightTextColor = [UIColor whiteColor];
    [self.imageView addSubview:titleLabel];
    [titleLabel release];
//返回按钮
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 30, 40, 30)];
    [self.imageView addSubview:backButton];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton release];
//添加按钮
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40 , 30, 30, 30)];
    [self.imageView addSubview:addButton];
    [addButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"addButton" ofType:@"png"]] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [addButton release];
}
//返回事件
- (void)buttonAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addButtonAction:(UIButton *)sender
{
    SetTimeViewController *setTime = [[SetTimeViewController alloc] init];
    setTime.delegate = self;
    [self presentViewController:setTime animated:YES completion:nil];
    [setTime release];
    
}

#pragma mark -
#pragma mark 创建提示的闹钟图标
- (void)createView
{
    UIImageView *alaremView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 50) / 2, 150, 50, 50)];
    [_imageView addSubview:alaremView];
    alaremView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"png"]];
    alaremView.nightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"png"]];
    [alaremView release];
    
    CGFloat width = alaremView.frame.origin.x - 80;
    CGFloat height = alaremView.frame.origin.y + alaremView.frame.size.height;
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(width, height + 10, self.view.frame.size.width - width* 2, 50)];
    title.textColor = [UIColor grayColor];
    title.nightTextColor = [UIColor whiteColor];
    title.text = @"没有服药提醒, 点击加号按钮添加一条提醒吧";
    title.textAlignment = NSTextAlignmentCenter;
    title.numberOfLines = 2;
    [_imageView addSubview:title];
    [title release];
}


//传值并创建table
- (void)createTable
{
    [self createTableView];
    NSString *path = [SaveTools pathWithName:@"alarm" directory:NSDocumentDirectory];
    _array = [NSMutableArray arrayWithContentsOfFile:path];
    [self.reciveArray removeAllObjects];
    [self.reciveArray addObjectsFromArray:_array];
    [self.tableView reloadData];
}

#pragma mark-
#pragma mark 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 100;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2.png" ofType:nil]]];
    self.tableView.nightBackgroundColor = UIColorFromRGB(0x343434);
    self.tableView.nightSeparatorColor = UIColorFromRGB(0x313131);
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.reciveArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlarmTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if (!cell) {
        cell = [[[AlarmTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"] autorelease];
    }
   
    NSString *onPath = [SaveTools pathWithName:@"on" directory:NSDocumentDirectory];
    NSString *str = [NSString stringWithContentsOfFile:onPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",str);
    if (str != nil) {
        cell.switchButton.on = YES;
    }else
    {
        NSLog(@"%@",str);
        cell.switchButton.on = NO;
    }
    cell.backgroundColor = [UIColor clearColor];
    [cell valueWithDic:[self.reciveArray objectAtIndex:indexPath.row]];
    return cell;
}

#pragma mark -
#pragma mark cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark -
#pragma mark 闹钟编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
   return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.reciveArray removeObjectAtIndex:indexPath.row];
        [tableView reloadData];

        NSString *path= [SaveTools pathWithName:@"alarm" directory:NSDocumentDirectory];
        NSFileManager *manager = [NSFileManager defaultManager];
        if ([manager fileExistsAtPath:path]) {
            NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];
            [array removeObjectAtIndex:indexPath.row];
            [array writeToFile:path atomically:YES];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
